# TODO: refactor root paramter on most functions

require "fileutils"

class Darcs

  def run_command(cmd)
    `darcs #{cmd}`
  end

  # Run darcs initialize
  def init
    run_command("initialize")
  end
  
  # Run darcs pull
  def pull
    run_command("pull")
  end     
    
  # Run whatsnew with summary and look-for-adds 
  def changes_and_new_files
    run_command("whatsnew -l -s")
  end
  
  # Run recursive add
  def add_all_new_files
    run_command("add -rq *")
  end
  
  # Record a new patch including all changes
  def record(name)
    run_command("record -a -m \"#{name}\"")
  end
  
  # Ignore Ignore
  def ignore(root, type)
    prepare_custom_boring_file_of_not_done_yet(root)
    case type
    when :db
      if File.exist?(root + "/config/database.example.yml")
        FileUtils.rm(root + "/config/database.yml", :force => true)
      else
        FileUtils.mv(root + "/config/database.yml", root + "/config/database.example.yml")
      end
      append_to_custom_boring_file(root, '^\config/database.yml$')
      append_to_custom_boring_file(root, '\.sqlite3$')
    when :tmp
      append_to_custom_boring_file(root, '^\tmp/cache($|/)')      
      append_to_custom_boring_file(root, '^\tmp/pids($|/)')      
      append_to_custom_boring_file(root, '^\tmp/sessions($|/)')      
      append_to_custom_boring_file(root, '^\tmp/sockets($|/)')      
    when :log
      append_to_custom_boring_file(root, '^\log/development.log$')
      append_to_custom_boring_file(root, '^\log/server.log$')
      append_to_custom_boring_file(root, '^\log/test.log$')
      append_to_custom_boring_file(root, '^\log/production.log$')
    end
  end
  
  def tag(name)
    run_command("tag --checkpoint #{name}")
  end
  
  def get_author(root)
    return false unless author_pref_file_already_exists?(root)
    File.open(root + "/_darcs/prefs/author", "r") do |f|
      f.readlines[0]
    end
  end
  
  def set_author(root, email)
    return false if author_pref_file_already_exists?(root)
    File.open(root + "/_darcs/prefs/author", "w") do |f|
      f << email
    end
  end
  
  def author_pref_file_already_exists?(root)
    return File.exist?(root + "/_darcs/prefs/author")
  end
  
  def is_darcs_repository?(root)
    unless File.exist?(root + "/_darcs")
      puts "Couldn't find darcs repository. You may 'rake darcs:bootstrap' your application first.."
      return false
    end
    true
  end
  
  def prepare_custom_boring_file_of_not_done_yet(root)
    return unless is_darcs_repository?(root)
    unless File.exist?(root + "/.boring")
      FileUtils.cp(root + "/_darcs/prefs/boring", root + "/.boring")
    end
    append_custom_boring_file_pref(root)
  end
  
  def append_to_custom_boring_file(root, entry)
    return if File.open(root + "/.boring", "r") { |f| f.readlines.join(" ").include?(entry) }
    File.open(root + "/.boring", "a+") do |f|
      f << entry + "\n"
    end
  end
  
  def append_custom_boring_file_pref(root)
    if File.exist?(root + "/_darcs/prefs/prefs") && 
       File.open(root + "/_darcs/prefs/prefs", "r") { |f| f.readlines.join(" ").include?('boringfile .boring')}       
      return 
    end
    File.open(root + "/_darcs/prefs/prefs", "a+") { |f| f << "boringfile .boring\n" }
  end
  
end
